<?php

namespace Admin\Controller;

use Admin\Controller\IndexController;
use Admin\Form\UserForm;
use Admin\Entity\User;
use Admin\Model\DBO\UserDBO;
use Zend\Form\Annotation\AnnotationBuilder;

class AddNewUserController extends IndexController {

    protected $form;

    public function getForm() {
        if (!$this->form) {
            $user = new UserForm();
            $builder = new AnnotationBuilder();
            $this->form = $builder->createForm($user);
        }
        return $this->form;
    }

    public function insertDataToForm($form) {
        // them ngay vao the ngay
        $valueOfDay = array();
        $valueOfDay[0] = 'Ngày:';
        for ($i = 1; $i <= 31; $i++) {
            $valueOfDay[$i] = $i;
        }
        $form->get('day')->setOptions(
                array(
                    'value_options' => $valueOfDay,
                )
        );
        $valueOfMonth = array();
        $valueOfMonth[0] = 'Tháng:';
        for ($i = 1; $i <= 12; $i++) {
            $valueOfMonth[$i] = 'Tháng ' . $i;
        }
        $form->get('month')->setOptions(
                array(
                    'value_options' => $valueOfMonth,
                )
        );
        $valueOfYear = array();
        $valueOfYear[0] = 'Năm:';
        for ($i = 2012; $i >= 1905; $i--) {
            $valueOfYear[$i] = $i;
        }
        $form->get('year')->setOptions(
                array(
                    'value_options' => $valueOfYear,
                )
        );
    }

    /*
     * Check login
     * getForm and send view layer
     */

    public function indexAction() {

        $this->checkRole1();
        // Lay thuc the dang nhap dua ra layout
        $this->layout()->identity = $this->getAuthService()->getIdentity();
        $form = $this->getForm();
        $this->insertDataToForm($form);
        $request = $this->getRequest();
        if ($request->isPost()) {
            $myMessage = array();
            $user = new User();
            $nonFile = $request->getPost()->toArray();
            $File = $this->params()->fromFiles('profilePhoto');
            $fileRename = "";
            $dirUpload = "";
            if (isset($File['name']) && $File['name'] != null) {
                $toArrayFileName = explode('.', $File['name']);
                $fileType = end($toArrayFileName);
                $fileRename = $request->getPost()->get('userName') . '.' . $fileType;
                $dirUpload = getcwd() . '/public/img/profilePhotos';
                $data = array_merge(
                        $nonFile, array('fileupload' => $File['name'])
                );
                $form->setData($data);
            } else {
                $form->setData($nonFile);
            }
            if ($form->isValid()) {
                // Dua du lieu vao doi tuong User
                $data = $form->getData();
                $user->setDataUser($data);
                $user->setBirthday($data['year'] . '-' . $data['month'] . '-' . $data['day']);
                if (isset($File['name']) && $File['name'] != null) {
                    $user->setProfilePhoto('/img/profilePhotos/' . $fileRename);
                }
                $userDBO = new UserDBO($this->getServiceLocator());
                if ($userDBO->saveUser($user)) {
                    if (isset($File['name']) && $File['name'] != null) {
                        $objectUpload = new UploadImage();
                        $objectUpload->upload($File, $form, $dirUpload, $fileRename);
                    }
                    $myMessage['saveOK'] = 'saveOK';
                } else {
                    $myMessage['userExist'] = 'userExist';
                }
            } else {
                foreach ($form->getMessages() as $value => $ms) {
                    $myMessage[$value] = $value;
                }
            }
            return array(
                'form' => $form,
                'myMessage' => $myMessage,
            );
        }
        return array(
            'form' => $form,
        );
    }

    public function editUserAction() {
        $userID = (int) $this->params()->fromRoute('id', 0);
        if (!$userID) {
            return $this->redirect()->toRoute('list-user');
        }
        $objectUserDBO = new UserDBO($this->getServiceLocator());
        $user = new User();
        $user->setDataUser($objectUserDBO->getUser($userID));
        $form = $this->getForm();
        $this->insertDataToForm($form);
        $form->bind($user);
        $form->get('register')->setAttribute('value', 'Chỉnh sửa');
        $request = $this->getRequest();
        if ($request->isPost()) {
            $nonFile = $request->getPost()->toArray();
            $File = $this->params()->fromFiles('profilePhoto');
            $fileRename = "";
            $dirUpload = "";
            if (isset($File['name']) && $File['name'] != null) {
                $toArrayFileName = explode('.', $File['name']);
                $fileType = end($toArrayFileName);
                $fileRename = $request->getPost()->get('userName') . '.' . $fileType;
                $dirUpload = getcwd() . '/public/img/profilePhotos';
                $data = array_merge(
                        $nonFile, array('fileupload' => $File['name'])
                );
                $form->setData($data);
            } else {
                $form->setData($nonFile);
            }
            if ($form->isValid()) {
                $user = $form->getData();
                $user->setBirthday($user->year . '-' . $user->month . '-' . $user->day);
                if (isset($File['name']) && $File['name'] != null) {
                    $user->setProfilePhoto('/img/profilePhotos/' . $fileRename);
                }
                $objectUserDBO->updateUserInfo($user, $userID);
                if (isset($File['name']) && $File['name'] != null) {
                    $objectUpload = new UploadImage();
                    $objectUpload->upload($File, $form, $dirUpload, $fileRename);
                }
                // Redirect to list of user
                return $this->redirect()->toRoute('list-user');
            }
        }
        return array(
            'userID' => $userID,
            'form' => $form,
        );
    }

    public function deleteUserAction() {
        $userID = (int) $this->params()->fromRoute('id', 0);
        if (!$userID) {
            return $this->redirect()->toRoute('list-user');
        }
        $objectUserDBO = new UserDBO($this->getServiceLocator());
        $objectUserDBO->deleteUser($userID);
        return $this->redirect()->toRoute('list-user');
    }

    

}
